/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Mr.Thanh
 */

// Check if string is non-blank
function isNonblank (s) {
   var isNonblank_re    = /\S/;
   return String (s).search (isNonblank_re) != -1
}

// Check if string is a whole number(digits only).
function isWhole (s) {
    var isWhole_re       = /^\s*\d+\s*$/;
   return String(s).search (isWhole_re) != -1
}

// check 0-9 digit
function regIsDigit(fData)
{var reg = /^[0-9]$/;
    return String(fData).search (reg) != -1
}


// checks that an input string is an integer, with an optional +/- sign character.
function isInteger (s) {
    var isInteger_re     = /^\s*(\+|-)?\d+\s*$/;
   return String(s).search (isInteger_re) != -1
}

// check is number
function regIsNumber(fData)
{var isInteger_re = /^[-]?[0-9]+[\.]?[0-9]+$/;  
   return String(fData).search (isInteger_re) != -1
}

// Checks that an input string is a decimal number, with an optional +/- sign character.
function isDecimal (s) {
    var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
   return String(s).search (isDecimal_re) != -1
}


// Check if string is currency
function isCurrency (s) {
    var isCurrency_re    = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
   return String(s).search (isCurrency_re) != -1
}

// checks that an input string looks like a valid email address.
function isEmail (s) {
    var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
   return String(s).search (isEmail_re) != -1;
}

// Check if string is a valid email address
function regIsEmail(fData)
  {
      var reg =/^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/;
      return String(fData).search (reg) != -1;
  }
  
  
  // Check for valid credit card type/number
var creditCardList = [
   //type      prefix   length
   ["amex",    "34",    15],
   ["amex",    "37",    15],
   ["disc",    "6011",  16],
   ["mc",      "51",    16],
   ["mc",      "52",    16],
   ["mc",      "53",    16],
   ["mc",      "54",    16],
   ["mc",      "55",    16],
   ["visa",    "4",     13],
   ["visa",    "4",     16]
];
function isValidCC (cctype, ccnumber) {
   var cc = getdigits (ccnumber);
   if (luhn (cc)) {
      for (var i in creditCardList) {
         if (creditCardList [i][0] == (cctype.toLowerCase ())) {
            if (cc.indexOf (creditCardList [i][1]) == 0) {
               if (creditCardList [i][2] == cc.length) {
                  return true;
               }
            }
         }
      }
   }
   return false;
}


function luhn (cc) {
   var sum = 0;
   var i;

   for (i = cc.length - 2; i >= 0; i -= 2) {
      sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)];
   }
   for (i = cc.length - 1; i >= 0; i -= 2) {
      sum += parseInt (cc.charAt (i), 10);
   }
   return (sum % 10) == 0;
}

// This returns a string with everything but the digits removed.
function getdigits (s) {
   return s.replace (/[^\d]/g, "");
}

// Get String Length
function regGetStrLength(fData)
{
    var valLength = fData.length;
    var reg = /^[\u0391-\uFFE5]$/;
    var result = 0;
    for(i=0; i< valLength; i++)
    {
        if(String(fData).search (reg) != -1)
        {
            result += 2;
        }
        else
        {
            result ++;
        }
    }
    return result;
}


function IsBoolean(value){
    if(value=="true"||value=="1"||value=="false"||value=="0")
        return true;
    return false;
}


function getField(data,index){
    var arr=data.split(',');
    var field=arr[index];
    var begin=field.indexOf("[")+1;
    var end=field.indexOf("]");    
    field=field.substr(begin,end-begin).trim("");
    return field;
}
function invalidate(data,value){
    var arr=data.split(',');
    var type=arr[2];
    var begin=type.indexOf("[")+1;
    var end=type.indexOf("]");
    
    type=type.substr(begin,end-begin).trim("");
    
    var allowNull=arr[3];
    begin=allowNull.indexOf("[")+1;
    end=allowNull.indexOf("]");
    allowNull=allowNull.substr(begin,end-begin).trim("");
    //neu ma thuoc tinh nay ko dc phep null ma gia tri no la rong thi loi
    if(allowNull.toUpperCase()=="NO" && value==""){  
          return true;
    }else if(allowNull.toUpperCase()=="NO" && value!=""){ 
        //kiem tra tiep kieu du lieu
        var t=type.substring(0,type.indexOf('('));
        var lengthLimit=type.substring(type.indexOf('(')+1,type.indexOf(')'));
        if((lengthLimit!="" && value.length>lengthLimit) ||value.length<1){
            return true;// do dai lon hon qui dinh
        }else{
             switch(t){                
                    
                case "int":
                    if(isNaN(value))
                        return true;
                    else
                        return false;
                    
                case "double":
                    if(isNaN(value))
                       return true;
                    else
                        return false;
                    
               case "datetime":
                    if(isInteger(value)!=-1)
                       return true;
                    else
                        return false;
                    
               case "timestamp":
                    if(isInteger(value)!=-1)
                       return true;
                    else
                        return false;
                    
               case "bit":
                    if(IsBoolean(value))
                       return false;
                    else
                        return true;
                     
             }
        }
    }     
    
    return false;
}